package com.example.llollama.service;

import com.example.llollama.user.ChatContext;
import com.example.llollama.user.InMemoryChatContextStore;
import com.example.llollama.util.PromptUtil;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class ContextChatService {
    private final ChatModel chatModel;

    public String chat(String sessionId, String userMessage) {
        // 获取上下文
        ChatContext context = InMemoryChatContextStore.getContext(sessionId);
        //ChatContext context = initContext();

        // 构建带上下文的输入
        String prompt = PromptUtil.buildPromptWithContext(context, userMessage);

        // 调用模型
        String call = chatModel.call(prompt);

        // 更新上下文
        context.addMessage("user: " + userMessage);
        context.addMessage("assistant: " + call);
        InMemoryChatContextStore.saveContext(context);

        return call;
    }

    public static ChatContext initContext() {
        ChatContext context = new ChatContext("9527");
        context.addMessage(
            "user:你好, 我是一名java开发工程师，熟练使用k8s，熟悉springboot，熟悉mysql，熟悉redis，熟悉docker，熟悉git，熟悉linux，熟悉java，熟悉spring，熟悉springmvc，熟悉springboot，熟悉springcloud，熟悉springsecurity,这样我是算是一个合格的java开发工程师吗");
        context.addMessage("assistant:是的, 你是一个合格的java开发工程师");
        return context;
    }
}
